package com.campus.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.campus.app.mapper.MenuMapper;
import com.campus.app.service.StudentsService;
import com.campus.app.service.TeachersService;
import com.campus.commons.entity.*;
import com.campus.app.mapper.UserMapper;
import com.campus.app.service.UsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.commons.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.validation.constraints.Min;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author Rin
 * @since 2020-12-24
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UsersService {

    @Autowired
    private StudentsService studentsService;
    @Autowired
    private TeachersService teachersService;

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //查询获取账号信息
        User vo = userMapper.selectUserByUsername(username);
        if (vo != null) {
            //获取账号的权限信息
            List<Menu> authorities = menuMapper.selectUserAuthorities(vo.getId());
            vo.setAuthorities(authorities);
        }
        return vo;
    }


    @Override
    public Result<User> getUserById(Integer id) {
        return Result.verify( userMapper.selectById(id));
    }

    @Override
    public Result isExistsByPhone(String phone) {
        LambdaQueryWrapper<User> queryWrapper = new QueryWrapper<User>().lambda();
        queryWrapper.eq(User::getPhone, phone);
        return Result.result(userMapper.selectCount(queryWrapper));
    }

    @Override
    public OnlyIdentification getIdentificationForUserId(Integer userId) {
        OnlyIdentification onlyIdentification=null;
        //先查学生；
        Teacher teacher = teachersService.getTeacherByUserId(userId);
        Student student = studentsService.getStudentByUserId(userId);
        if (teacher != null) {
            onlyIdentification = new OnlyIdentification(teacher.getId(),
                    IdentificationType.RoleType_1, teacher.getTeachername());
        } else if (student != null) {
            onlyIdentification = new OnlyIdentification(student.getId(),
                    IdentificationType.RoleType_2, student.getStuname());
        } else {
            throw new RuntimeException("执行UserService的getIdForUserId方法错误，原因：找不到该userId对应的身份");
        }
        return onlyIdentification;
    }

    @Override
    public Result upDeId(@Min(1) int userId, int deId) {
       return Result.result(userMapper.updateById(new User(userId,deId)));
    }

    @Override
    public int UpdateUser(User user) {
        int update = userMapper.updateById(user);
        return update;
    }

}

